//
//  HYSeatBookingDetailVC.m
//  Booking
//
//  Created by Duntech on 2018/4/23.
//  Copyright © 2018年 Duntech. All rights reserved.
//

#import "HYSeatBookingDetailVC.h"
#import "HYBookSuccessView.h"
#import "HYReserveSeatInfoTableViewCell.h"
#import "HYMeetingScheduleVC.h"

@interface HYSeatBookingDetailVC ()

@property (nonatomic,strong) UIButton *confirmBtn;
@property (nonatomic,strong) UIButton *addReserveBtn;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIView *lineView;
/** dataSource */
@property (nonatomic,strong) HYTableViewDataSource *dataSource;
/** delegate */
@property (nonatomic,strong) HYTableViewDelegate *delegate;
/** dataSets数据源 */
@property (nonatomic,strong) NSMutableArray *dataSetsArray;
@property (nonatomic,strong) NSMutableArray *sectionArray;

@end

@implementation HYSeatBookingDetailVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self setupSubviews];
    
    [self setupData];
    
    [self bindingTableViewCell];
}

- (void)setupSubviews{
    
    self.title = NSLocalizedString(@"confirmSeatBooking", nil);
    self.view.backgroundColor = KAPP_WHITE_COLOR;
    self.dataSource.dataSource = self.dataSetsArray;
    self.delegate.dataSource = self.dataSetsArray;
    [self.dataSetsArray addObject:self.sectionArray];
    [self.view addSubview:self.lineView];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.confirmBtn];
//    [self.view addSubview:self.addReserveBtn];
    
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(self.view);
        make.height.mas_equalTo(1);
    }];
    
    [_confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(15 * WIDTH_MULTIPLE);
        make.right.equalTo(self.view).offset(-15 * WIDTH_MULTIPLE);
        make.bottom.equalTo(self.view).offset(-25 * WIDTH_MULTIPLE);
        make.height.mas_equalTo(46 * WIDTH_MULTIPLE);
    }];
    
//    [_addReserveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(self.confirmBtn.mas_left).offset(-12 * WIDTH_MULTIPLE);
//        make.left.equalTo(self.view.mas_left).offset(15 * WIDTH_MULTIPLE);
//        make.bottom.equalTo(self.confirmBtn.mas_bottom);
//        make.top.equalTo(self.confirmBtn.mas_top);
//    }];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.lineView.mas_bottom);
        make.bottom.equalTo(self.confirmBtn.mas_top);
    }];
}

- (void)setupData{
    [self.tableView registerNib:[UINib nibWithNibName:@"HYReserveSeatInfoTableViewCell" bundle:nil] forCellReuseIdentifier:@"HYReserveSeatInfoTableViewCell"];
    
    self.seatModel.cellIdientifier = @"HYReserveSeatInfoTableViewCell";
    self.seatModel.cellHeight = 85;
    
    [self.sectionArray addObject:self.seatModel];
    [self.tableView reloadData];
}

- (void)bindingTableViewCell{
    
    __weak typeof(self) weakSelf = self;
    [self.dataSource setBindingCellBlock:^(UITableView *tableView, id rowDataSource, HYBaseTableViewCell *cell, NSIndexPath *indexPath) {
        HYReserveSeatInfoTableViewCell *seatInfoCell = (HYReserveSeatInfoTableViewCell *)cell;
        seatInfoCell.deleteBlock = ^{
            [weakSelf.sectionArray removeObjectAtIndex:indexPath.row];
            [tableView reloadData];
        };
        seatInfoCell.editBlock = ^{
            HYMeetingScheduleVC *scheduleVC = [HYMeetingScheduleVC new];
            scheduleVC.seatModel = weakSelf.sectionArray[indexPath.row];
            scheduleVC.scheduleType = SeatEditSchedule;
            scheduleVC.SeatTimeEditBlock = ^(HYSeatCellModel *model) {
                weakSelf.seatModel = model;
                [weakSelf.sectionArray replaceObjectAtIndex:indexPath.row withObject:model];
                [weakSelf.tableView reloadData];
            };
            [weakSelf presentViewController:scheduleVC animated:YES completion:nil];
        };
    }];
}

#pragma mark - action
- (void)confirmBtnAction{
    
//    for (NSInteger i = 0; i < self.dataValueArray.count; i++) {
//
//        NSString *value = self.dataValueArray[i];
//        if (![value isNotBlank]) {
//
//            NSString *text = self.textFieldArray[i];
//            [JRToast showWithText:[NSString stringWithFormat:@"%@ must be not null",[text substringToIndex:text.length - 1]] duration:2];
//            return;
//        }
//    }
//
//    if (![self.timeValueArray[0] isNotBlank]) {
//
//        [JRToast showWithText:[NSString stringWithFormat:@"%@ must be not null",@"Start time"] duration:2];
//        return;
//    }
//    if (![self.timeValueArray[1] isNotBlank]) {
//
//        [JRToast showWithText:[NSString stringWithFormat:@"%@ must be not null",@"End time"] duration:2];
//        return;
//    }
//
//    NSDate *startDate = [NSDate dateWithString:self.timeValueArray[0] format:@"yyyy-MM-dd HH:ss"];
//    NSInteger startTimestamp = [startDate timeIntervalSince1970];
//    NSDate *endDate = [NSDate dateWithString:self.timeValueArray[1] format:@"yyyy-MM-dd HH:ss"];
//    NSInteger endTimeStamp = [endDate timeIntervalSince1970];
//    if (endTimeStamp - startTimestamp < 60 * 60) {
//
//        [JRToast showWithText:@"endTime must be greater than 30 min" duration:2];
//        return;
//    }
//    if (endTimeStamp - startTimestamp > 24 * 60 * 60) {
//
//        [JRToast showWithText:@"meeting must be in 4 Hour" duration:2];
//        return;
//    }
//
    NSString *startDayTime = [NSString stringWithFormat:@"%@:00",self.seatModel.startTime];
    NSString *endDayTime = [NSString stringWithFormat:@"%@:00",self.seatModel.endTime];
    [HYNetRequestHandle bookingSeatWithSeatID:self.seatModel.id currdate:self.seatModel.meetingDate startDayTime:startDayTime endDayTime:endDayTime remark:nil Complection:^(BOOL isSuccess) {

        if (isSuccess) {
            HYBookSuccessView *bookSuccessView = [[HYBookSuccessView alloc] initwithTitle:NSLocalizedString(@"seatBookedSuccessfully", nil)];
            [bookSuccessView showBookSuccessView];
            bookSuccessView.bookSuccessBlock = ^{

                NSArray *pushVCAry = [self.navigationController viewControllers];
                UIViewController *popVC = [pushVCAry objectAtIndex:0];
                [self.navigationController popToViewController:popVC animated:YES];
                //发通知跳到我的预定
                [[NSNotificationCenter defaultCenter] postNotificationName:KBookSussessNotification object:nil];
            };

        }
    }];
}

#pragma mark - lazyload
- (HYTableViewDataSource *)dataSource{
    
    if (!_dataSource) {
        _dataSource = [HYTableViewDataSource new];
    }
    return _dataSource;
}

- (NSMutableArray *)dataSetsArray{
    
    if (!_dataSetsArray) {
        _dataSetsArray = [NSMutableArray array];
    }
    return _dataSetsArray;
}

- (NSMutableArray *)sectionArray{
    
    if (!_sectionArray) {
        _sectionArray = [NSMutableArray array];
    }
    return _sectionArray;
}

- (HYTableViewDelegate *)delegate{
    
    if (!_delegate) {
        _delegate = [HYTableViewDelegate new];
    }
    return _delegate;
}

-(UIView *)lineView{
    if (!_lineView) {
        _lineView = [[UIView alloc]init];
        _lineView.backgroundColor = KAPP_SEPERATOR_COLOR;
    }
    return _lineView;
}

- (UITableView *)tableView{
    
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = KAPP_WHITE_COLOR;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self.delegate;
        _tableView.dataSource = self.dataSource;
    }
    return _tableView;
}

- (UIButton *)confirmBtn{
    
    if (!_confirmBtn) {
        
        _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_confirmBtn setBackgroundColor:KAPP_THEME_COLOR];
        [_confirmBtn setTitle:NSLocalizedString(@"confirm", nil) forState:UIControlStateNormal];
        [_confirmBtn setTitleColor:KAPP_WHITE_COLOR forState:UIControlStateNormal];
        _confirmBtn.titleLabel.font = KFitFont(20);
        _confirmBtn.layerCornerRadius = 4;
        [_confirmBtn addTarget:self action:@selector(confirmBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _confirmBtn;
}

-(UIButton *)addReserveBtn{
    if (!_addReserveBtn) {
        _addReserveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_addReserveBtn setBackgroundColor:KAPP_WHITE_COLOR];
        [_addReserveBtn setTitle:@"增加预定" forState:UIControlStateNormal];
        [_addReserveBtn setTitleColor:KAPP_THEME_COLOR forState:UIControlStateNormal];
        _addReserveBtn.titleLabel.font = KFitFont(20);
        _addReserveBtn.layerCornerRadius = 4;
        _addReserveBtn.layerBorderWidth = 1;
        _addReserveBtn.layerBorderColor = KAPP_THEME_COLOR;
        [_addReserveBtn addTarget:self action:@selector(confirmBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addReserveBtn;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
